/*Combining data sets for analysis*/

noi disp "Obtaining initial shares data..."
noi disp "Verifying that BartikAnalyticData_2digit.dta does not already exist..."
* This returns an error if the file does not exist.
capture confirm file BartikAnalyticData_2digit.dta

* If an error is returned, then the commands below are run.
if _rc == 601 {
	noi disp "Data not found.  Building data..."
	noi disp "Building BartikAnalyticData_2digit.dta..."


	noi disp "Loading CountyBanking.dta..."
	use "$LocalData\CountyBanking.dta", clear	
	noi disp "Merging CountyIndustryEmp_2digit.dta..."
	sort geography year
	merge geography year using "$LocalData\CountyIndustryEmp_2digit.dta"
	drop _merge
	sort geography year

	noi disp "Merging Demographic.dta..."
	merge geography year using "$LocalData\Demographic.dta"

	sort geography year
	*Checking unique geography years. 
	by geography year: gen nobs = _N
	su nobs
	drop nobs
	
	*Initial demographic data 
	keep if year>=2000
	*Restrict to observations with first observations in 2000. Lose 58 of 57,827 county-years.
	bysort geography: egen minyear = min(year)
	keep if minyear==2000
	drop minyear
	by geography: gen br2000 = cnty_br[1]
	by geography: gen emp2000 = c_emp[1]
	
	
	*Keeping only counties observed in 2000

	drop if missing(emp2000) | missing(br2000)


	*Some state IDs got lost in the mergers	
	gen state=floor(geography/1000)
	
	drop _merge

	merge m:1 geography using "$LocalData\Cluster3digit.dta"
	drop _merge
	
	*Saving Analytical Dataset	
	compress
	save "$LocalData\BartikAnalyticData_2digit.dta", replace
	save "$LocalData\Archive\BartikAnalyticData_2digit`CurrentDate'.dta", replace

}
* This runs if no error was returned.
else noi disp "Data already exists."
noi etime
noi disp " "
